//
//  BuyView.m
//  News
//
//  Created by weige on 16/6/5.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "BuyView.h"
#import "Goods.h"
#import "WYAddToCartAnimation.h"
#import "ShopCarTools.h"
@interface BuyView()
{
   
    Goods *good;
    UILabel *buhuo;
    
}
@end
@implementation BuyView
@synthesize addGoodNumber ,subtractionGoofNumber,labNumber,number;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUIbuyView];
        self.isUserflag=NO;
        
    }
    return self;
}

-(void)createUIbuyView
{
    addGoodNumber=[UIButton buttonWithType:UIButtonTypeCustom];
    [addGoodNumber setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [addGoodNumber addTarget:self action:@selector(addGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    addGoodNumber.hidden=YES;
    [self addSubview:addGoodNumber];
    labNumber=[[UILabel alloc]init];
    labNumber.font=[UIFont systemFontOfSize:10 weight:2];
    labNumber.textColor=[UIColor redColor];
    labNumber.hidden=YES;
    labNumber.textAlignment=NSTextAlignmentCenter;
    [self addSubview:labNumber];
    subtractionGoofNumber=[UIButton buttonWithType:UIButtonTypeCustom];
    [subtractionGoofNumber setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    [subtractionGoofNumber addTarget:self action:@selector(subtractionGoofNumberclick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:subtractionGoofNumber];
    subtractionGoofNumber.hidden=YES;
    buhuo=[[UILabel alloc]init];
    buhuo.font=[UIFont systemFontOfSize:11 weight:2];
    buhuo.textColor=[UIColor redColor];
    buhuo.textAlignment=NSTextAlignmentCenter;
    buhuo.hidden=YES;
    
    [self addSubview:buhuo];
}



-(void)layoutSubviews
{

    [super layoutSubviews];
    //NSLog(@"%@",good);
    addGoodNumber.frame=CGRectMake(self.frame.size.width-self.frame.size.height-4, 0, self.frame.size.height, self.frame.size.height);
    labNumber.frame=CGRectMake(CGRectGetMinX(addGoodNumber.frame)-25, 0, 25, self.frame.size.height);
    subtractionGoofNumber.frame=CGRectMake(CGRectGetMinX(labNumber.frame)-self.frame.size.height, 0, self.frame.size.height, self.frame.size.height);
    buhuo.frame=CGRectMake(CGRectGetMaxX(subtractionGoofNumber.frame), 0, 60, 20);
    
}

-(void)setGoods:(Goods *)goods
{

    good=goods;
    if ([ShopCarTools sharInstance].goodArr.count==0) {
        
        number=0;
    }
    NSLog(@"这里值%lu",(unsigned long)[ShopCarTools sharInstance].goodArr.count);
    for (Goods *g in [ShopCarTools sharInstance].goodArr) {
        
        NSLog(@"%d",g.userBuyNumber);
        if (g.ID==goods.ID) {
            good.userBuyNumber=g.userBuyNumber;
            number=good.userBuyNumber;
            labNumber.hidden=NO;
            labNumber.text=[NSString stringWithFormat:@"%d",number];

        }
        else
        {
            number=good.userBuyNumber;
        }
    }
   
    NSLog(@"为什么还有值%d",number);
    //self.goods=goods;
   // number=good.number;
    if (goods.number>0) {
        addGoodNumber.hidden=NO;
        subtractionGoofNumber.hidden=NO;
        labNumber.hidden=NO;
        buhuo.hidden=YES;
        
        //labNumber.text=@"112";
    }
    else
    {
        buhuo.hidden=NO;
        addGoodNumber.hidden=YES;
        subtractionGoofNumber.hidden=YES;
        labNumber.hidden=YES;
        buhuo.text=@"正在补货";
    }
    //NSLog(@"number===%d",number);
    if (number==0) {
        labNumber.hidden=YES;
        subtractionGoofNumber.hidden=YES;
        
    }
    else
    {
        labNumber.hidden=NO;
        subtractionGoofNumber.hidden=NO;
    }
 
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    NSLog(@"%@",change);
}
-(void)dealloc
{
    [good removeObserver:self forKeyPath:@"userBuyNumber"];
}
-(void)addGoodsButtonClick:(UIButton *)sender
{
    //NSLog(@"添加商品");
    WYAddToCartAnimation *an=[[WYAddToCartAnimation alloc]init];
   // NSLog(@"%@",good);
    
   
    if (number<good.number) {
         number++;
        good.userBuyNumber=number;
        [an addToCartAnimationWithImageView:self.imageGood];
        self.subtractionGoofNumber.hidden=NO;
        self.labNumber.hidden=NO;
       // NSLog(@"爱鲜%@",good.name);
        self.labNumber.text=[NSString stringWithFormat:@"%d",number];
        [[ShopCarTools sharInstance] addShopCarProduct:good];
    }
    else
    {
      
    }
   // NSLog(@"%d",good.number);
    
    
}

-(void)subtractionGoofNumberclick:(UIButton *)sender
{
    number--;
    good.userBuyNumber=number;
    if (number<=0) {
        
        self.subtractionGoofNumber.hidden=YES;
        self.labNumber.hidden=YES;
       
    }
    else
    {
    
        self.labNumber.text=[NSString stringWithFormat:@"%d",number];
        
    }
    
    WYRedDotView *red=[WYRedDotView sharedInstance];

    [red reduceFromCard];
    if (self.isUserflag==NO) {
        [[ShopCarTools sharInstance] removeShopCarProduct:good andIsUserFlag:NO];
        
    }
    else
    {
        [[ShopCarTools sharInstance] removeShopCarProduct:good andIsUserFlag:YES];
    }
    
    [[NSNotificationCenter  defaultCenter]postNotificationName:@"goodsubtraction" object:nil];
}
@end
